﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace 关机
{
    public partial class Form1 : Form
    {
        public void CMDOperator(string cmd)
        {
            #region 操作cmd
            //1.利用Process打开一个cmd的进程
            Process process_cmd = new Process();//创建进程对象
            process_cmd.StartInfo.FileName = "cmd.exe";//进程打开文件名为“cmd”

            //2.配置输入输出许可
            process_cmd.StartInfo.RedirectStandardError = true;// 重定向错误流
            process_cmd.StartInfo.RedirectStandardInput = true;// 重定向输入流
            process_cmd.StartInfo.RedirectStandardOutput = true;// 重定向输出流

            //3.设置不创建窗体，不使用系统shell
            process_cmd.StartInfo.CreateNoWindow = true;// TRUE隐藏窗口运行；false在winform下会弹出
            process_cmd.StartInfo.UseShellExecute = false;//是否使用系统shell执行，否

            //4.进程开启
            process_cmd.Start();
            //5.CMD执行命令
            process_cmd.StandardInput.WriteLine(cmd + "&exit");//加上"&exit"防止进程死循

            #region 测试
            //string strOuput = process_cmd.StandardOutput.ReadToEnd();//获取输出信息
            //string strerr = process_cmd.StandardError.ReadToEnd();//获取错误信息
            //if (strerr != "")//注意""和null不相等
            //{
            //    richTextBox1.Text = strerr;
            //    MessageBox.Show("报错");
            //}
            //else if (strOuput != "")
            //{
            //    richTextBox1.Text = strOuput;
            //    MessageBox.Show("正常");
            //}
            #endregion 测试
            process_cmd.WaitForExit();//等待程序执行完退出进程
            process_cmd.Close();//关闭进程
            #endregion 操作cmd
            this.Hide();
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            CMDOperator("shutdown -l");//执行注销命令

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            CMDOperator("shutdown -s -f -t 0");//执行强制关机命令
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            CMDOperator("shutdown -r -f -t 0");//执行强制重启计算机命令
            
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            CMDOperator("shutdown -h");//执行休眠计算机命令
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
           pictureBox1.BackColor= Color.White;
        }

        private void pictureBox5_MouseLeave(object sender, EventArgs e)
        {
            pictureBox5.BackColor = Color.LightGray;
        }

        private void pictureBox4_MouseEnter(object sender, EventArgs e)
        {
            pictureBox4.BackColor = Color.White;
        }

        private void pictureBox4_MouseLeave(object sender, EventArgs e)
        {
            pictureBox4.BackColor = Color.LightGray;
        }

        private void pictureBox5_MouseEnter(object sender, EventArgs e)
        {
            pictureBox5.BackColor = Color.White;
        }

        private void pictureBox3_MouseEnter(object sender, EventArgs e)
        {
            pictureBox3.BackColor = Color.White;
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            pictureBox3.BackColor = Color.LightGray;
        }

        private void pictureBox2_MouseEnter(object sender, EventArgs e)
        {
            pictureBox2.BackColor = Color.White;
        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            pictureBox2.BackColor = Color.LightGray;
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.White;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.BackColor = Color.LightGray;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //异步委托实现启动即隐藏窗口
            this.BeginInvoke(new Action(() =>
            {
                this.Hide();

            }));
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void 注销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1_Click(sender, e);
        }

        private void 关机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox2_Click(sender, e);
        }

        private void 重启ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox3_Click(sender, e);
        }

        private void 休眠ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox4_Click(sender, e);
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //CMDOperator("shutdown");
        }
    }
}